package com.blu.config;
import java.util.HashMap;
import java.util.Map;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.blu.realm.AccountRealm;
import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;

@Configuration
public class ShiroConfig {
	//Qualifier  https://www.cnblogs.com/felordcn/p/12142471.html
	@Bean
	public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager securityManager){
		ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
		factoryBean.setSecurityManager(securityManager);
		Map<String,String> map = new HashMap<String, String>();
		map.put("/main", "authc");
		map.put("/manage","perms[manage]");//权限
		map.put("/admin", "roles[admin]");//角色
		factoryBean.setFilterChainDefinitionMap(map);
		factoryBean.setLoginUrl("/login");
		factoryBean.setUnauthorizedUrl("/unauthor");
		return factoryBean;
	}
	
	@Bean
	public DefaultWebSecurityManager securityManager(@Qualifier("accoutRealm") AccountRealm accountRealm){
		DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
		manager.setRealm(accountRealm);
	  	return manager;
	}
	
	@Bean
	public AccountRealm accoutRealm(){
		return new AccountRealm();
	}
	@Bean
	public ShiroDialect shiroDialect(){
		return new ShiroDialect();
	}
}
